package com.supermap.dataservice.order;


import com.supermap.dataservice.order.dao.ServiceDao;
import com.supermap.dubbo.api.order.entity.Service;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.order.ServiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:功能逻辑处理层
 * @Date:18/6/19 上午10:45
 */
@com.alibaba.dubbo.config.annotation.Service
public class ServiceServiceImpl implements ServiceService {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(ServiceServiceImpl.class);

    @Resource
    private ServiceDao serviceDao;


    @Override
    public JSONUtil getUserService(String userid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            List<Service> services = serviceDao.getAllServiceByUserId(Integer.parseInt(userid));
            HashMap serviceHM = new HashMap();
            if(services!=null && services.size()>0){
                List<Service> orderEd = new ArrayList<>();
                List<Service> noOrder = new ArrayList<>();
                for (Service s:services) {
                    if("1".equals(s.getFlag()) || s.getId()==1 || s.getId()==2 || s.getId()==3){
                        orderEd.add(s);
                    }else{
                        noOrder.add(s);
                    }
                }
                serviceHM.put("orderEd",orderEd);
                serviceHM.put("noOrder",noOrder);
            }
            //获得用户所有的服务信息后返回的json数据
            jsonUtil.setMsg("Get user services success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(serviceHM);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("ServiceServiceImpl=>getUserService=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get user services flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil addUserService(String userid, String serviceid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            serviceDao.addUserService(Integer.parseInt(userid),Integer.parseInt(serviceid));

            //增加用户的功能定制后返回的json数据
            jsonUtil.setMsg("add user services success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData("");
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("ServiceServiceImpl=>addUserService=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("add user services flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil checkUserServiceExist(String userid, String serviceid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            List<Service> services = serviceDao.checkUserServiceExist(Integer.parseInt(userid),Integer.parseInt(serviceid));

            if(services!=null && services.size()>0){
                jsonUtil.setData(true);
            }else{
                jsonUtil.setData(false);
            }
            //增加用户的服务定制后返回的json数据
            jsonUtil.setMsg("check user services success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);

        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("ServiceServiceImpl=>checkUserServiceExist=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("check user services flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delUserService(String userid, String serviceid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            serviceDao.delUserService(Integer.parseInt(userid),Integer.parseInt(serviceid));

            //删除用户服务定制后返回的json数据
            jsonUtil.setMsg("del user services success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData("");
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("ServiceServiceImpl=>delUserService=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("del user services flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
}
